import 'package:flutter/material.dart';

import '../../dao/health_data_dao.dart';
import 'health_data_chart_module.dart';
import 'health_data_model.dart';

class PersonalHealthPage extends StatefulWidget {
  const PersonalHealthPage({super.key});

  @override
  State<PersonalHealthPage> createState() => _PersonalHealthPageState();
}

class _PersonalHealthPageState extends State<PersonalHealthPage>
    with TickerProviderStateMixin {
  late TabController _tabController;
  late List<HealthDataModule> _tabs;

  late Future<HealthDataModel> _healthData;

  @override
  void initState() {
    _tabController = TabController(length: 4, vsync: this);
    _healthData = HealthDataDao.fetchMyData();
    _tabs = [
      HealthDataModule(
        name: "日",
        summaryTitle: "今日",
        runHealthData: _healthData,
      ),
      HealthDataModule(
        name: "周",
        summaryTitle: "平均",
        runHealthData: _healthData,
      ),
      HealthDataModule(
        name: "月",
        summaryTitle: "平均",
        runHealthData: _healthData,
      ),
      HealthDataModule(
        name: "年",
        summaryTitle: "平均",
        runHealthData: _healthData,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('数据'),
      ),
      body: Column(
        children: [
          Container(
            child: TabBar(
              isScrollable: true,
              controller: _tabController,
              labelStyle: TextStyle(
                  fontSize: 18,
                  color: Colors.blue,
                  fontWeight: FontWeight.bold),
              unselectedLabelStyle: TextStyle(fontSize: 18),
              labelColor: Colors.lightBlue,
              unselectedLabelColor: Colors.black,
              tabAlignment: TabAlignment.center,
              indicatorColor: Colors.white,
              indicatorWeight: 0,
              indicator: BoxDecoration(
                  borderRadius: BorderRadius.circular(45),
                  shape: BoxShape.rectangle,
                  color: Colors.white),
              indicatorSize: TabBarIndicatorSize.label,
              tabs: _tabs.map<Tab>(
                (HealthDataModule tab) {
                  return Tab(
                    text: tab.name,
                  );
                },
              ).toList(),
            ),
          ),
          Flexible(
            child: TabBarView(controller: _tabController, children: _tabs),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }
}
